﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HouseList : System.Web.UI.Page
{
    private int _userId;
    public int UserId
    {
        get { return _userId; }
        set { _userId = value; }
    }

    private int _userType;
    public int UserType
    {
        get { return _userType; }
        set { _userType = value; }
    }

    private int _pId;
    public int PId
    {
        get { return _pId; }
        set { _pId = value; }
    }

    public PersonalInformation pInfo;

    private List<House> list;

    public List<House> List
    {
        get { return list; }
        set { list = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Test Code
        Session["UserType"] = 0;
        Session["UserId"] = 2;
        Session["pId"] = 1004;

        // Normal 
        AccountChecked();

        if (Session["UserType"] == null || Session["UserId"] == null || Session["pId"] == null)
            Response.Redirect("~/Home.aspx");
        else
        {
            UserId = Int32.Parse(Session["UserId"].ToString());
            UserType = Int32.Parse(Session["UserType"].ToString());
            PId = Int32.Parse(Session["pId"].ToString());
        }
        if (IsPostBack)
            return;
        list = HouseManage.GetAllHouses(UserId);
        Bind();
        Page.DataBind();
    }

    private void Bind()
    {
        this.rptHouseList.DataSource = List;
        this.rptHouseList.DataBind();
    }
    protected void CheckAll(object sender, EventArgs e)
    {
        SetCheckBoxChecked(this, true);
    }

    private void SetCheckBoxChecked(Control control, bool check)
    {
        if (control is CheckBox)
        {
            ((CheckBox)control).Checked = check;
        }
        else if(control.HasControls())
        {
            foreach (Control s in control.Controls)
            {
                SetCheckBoxChecked(s, check);
            }
        }
    }
    protected void Publish(object sender, EventArgs e)
    {
        List<int> list = GetAllCheckedIndex();
        foreach(int hId in list)
            HouseManage.ChangeStatus(hId, (int)StatusCodeName.已发布);
        List = HouseManage.GetAllHouses(UserId);
        Bind();
    }

    private List<int> GetAllCheckedIndex()
    {
        List<int> list = new List<int>();
        GetAllChecked(this,list);
        return list;
    }

    private void GetAllChecked(Control control, List<int> list)
    {
        if (control is CheckBox)
        {
            if (((CheckBox)control).Checked)
                list.Add(Convert.ToInt32(((CheckBox)control).Attributes["HouseId"]));
        }
        else if (control.HasControls())
        {
            foreach (Control s in control.Controls)
            {
                GetAllChecked(s, list);
            }
        }
    }
    protected void Cancel(object sender, EventArgs e)
    {
        List<int> list = GetAllCheckedIndex();
        foreach (int hId in list)
            HouseManage.ChangeStatus(hId, (int)(StatusCodeName.已撤销));
        List = HouseManage.GetAllHouses(UserId);
        Bind();
    }
    protected void Delete(object sender, EventArgs e)
    {
        List<int> list = GetAllCheckedIndex();
        foreach (int hId in list)
            HouseManage.ChangeStatus(hId, (int)(StatusCodeName.已删除));
        List = HouseManage.GetAllHouses(UserId);
        Bind();
    }

    protected void Rented(object sender, EventArgs e)
    {
        List<int> list = GetAllCheckedIndex();
        foreach (int hId in list)
            HouseManage.ChangeStatus(hId, (int)(StatusCodeName.已租出));
        List = HouseManage.GetAllHouses(UserId);
        Bind();
    }

    protected void Inverse(object sender, EventArgs e)
    {
        InverseAllCheckBox(this);
    }

    private void InverseAllCheckBox(Control control)
    {
        if (control is CheckBox)
        {
            ((CheckBox)control).Checked = !((CheckBox)control).Checked;
        } else if (control.HasControls())
        {
            foreach (Control s in control.Controls)
            {
                InverseAllCheckBox(s);
            }
        }
    }

    protected void StatusFilter(object sender, EventArgs e)
    {
        // 获取已选状态
        DropDownList ddl = (DropDownList)sender;
        string itemName = ddl.SelectedValue;
        // 获取已选状态对应数据
        if (itemName == "全部")
            List = HouseManage.GetAllHouses(UserId);
        else
        {
            for (int cnt = 0; cnt < Enum.GetValues(typeof(StatusCodeName)).Length; cnt++)
            {
                if (itemName == ((StatusCodeName)cnt).ToString())
                    List = HouseManage.GetHousesByStatus(UserId, cnt);
            }
        }
        // 绑定数据
        Bind();        
    }

    protected void AccountChecked()
    {
        if (Session["UserType"] != null && Session["UserId"] != null)
        {
            if (Convert.ToInt32(Session["UserType"]) == 0)
            {
                int id = Convert.ToInt32(Session["UserId"]);
                if (!HouseHolderManage.IsChecked(id))
                {
                    // 跳转 提示页面
                    Response.Redirect("/Error.aspx");
                }
            }
        }
    }
}